package common.sysconfig;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static common.util.VerifyUtil.isServer;

@Configuration
public class ElasticSearchConfiguration {


    @Value("${minio.es-host}")
    private String esHost;

    @Bean
    public ElasticsearchClient restClient() {
        RestClient restClient = null;
        if (isServer()) {
            System.out.println("调用了服务器的地址+es+" + esHost);
            restClient = RestClient.builder(new HttpHost(esHost, 19201)).build();
        } else {
            System.out.println("调用了服务器的地址+es+" + "127.0.0.1");

            restClient = RestClient.builder(new HttpHost("127.0.0.1", 19201)).build();
        }

        // 使用Jackson映射器创建传输层
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        // 创建API客户端
        return new ElasticsearchClient(transport);
    }

}
